package com.llkj.qingnianguoxue.book;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.BookMasterAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.bean.BookVideoBean;
import com.llkj.qingnianguoxue.bean.BookVideoListBean;
import com.llkj.qingnianguoxue.bean.EventChangeUserBean;
import com.llkj.qingnianguoxue.home.MasterDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

/**
 * 订阅大师
 */
public class BookMastersFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private BookMasterAdapter masterAdapter;
    private List<BookVideoBean> mMasterData;
    private int totalSize, mPage, mCurrentPosition;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_book_masters, container, false);
        EventBus.getDefault().register(this);
        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
        return rootView;
    }

    private void initView() {
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvActions = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        lvActions.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        mMasterData = new ArrayList<>();
        masterAdapter = new BookMasterAdapter(R.layout.item_book_master, mMasterData);
        lvActions.setAdapter(masterAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        masterAdapter.setOnLoadMoreListener(this);
        lvActions.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                BookVideoBean masterBean = mMasterData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("id", masterBean.getId());
                startActivity(MasterDetailActivity.class, bundle);
            }
        });

        RxBus.getInstance().toObservable(String.class).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                if (s.equals(ConstantUserInfo.REFRESH_SUBSCRIBE)){
                    onRefresh();
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeUser(EventChangeUserBean event){
        onRefresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getVideoData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getVideoData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, masterAdapter, null, lvActions, totalSize);
    }

    private void getVideoData() {
        HttpMethod.userbuyArtist(getActivity(), this, "", String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_USERBUY_ARTIST) {
            BookVideoListBean hotRankingBean = GsonUtil.GsonToBean(data, BookVideoListBean.class);
            if (hotRankingBean != null) {
                totalSize = Integer.parseInt(hotRankingBean.getNum());
                if (mPage == 1) {
                    mMasterData.clear();
                    LoadDataHelper.refreshComplete(masterAdapter, mMasterData, refreshLayout, lvActions);
                } else {
                    masterAdapter.setEnableLoadMore(true);
                    masterAdapter.loadMoreComplete();
                }
                mMasterData.addAll(hotRankingBean.getArtist());
                masterAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        refreshLayout.setRefreshing(false);
    }
}
